﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;

namespace Portal.CDSP
{
    public class WebLinkBLL
    {
        public WebLinkCls CreateView()
        {
            string Query = "select top 6 * from SODCWebLinks ";

            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;

            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                WebLinkCls WebCls = new WebLinkCls();
                WebCls.WebLinkId = (string)dtResult.Rows[0]["WebLinkID"];
                try
                {
                    WebCls.WeblinkName = (string)dtResult.Rows[0]["WebLinkName"];
                }
                catch { }
                try
                {

                    WebCls.belongSite = (string)dtResult.Rows[0]["belongsite"];
                }
                catch { }

                try
                {
                    WebCls.WebLinkUrl = (string)dtResult.Rows[0]["WebLinkUrl"];
                }
                catch { }

                try
                {
                    WebCls.IndexOrder = (int)dtResult.Rows[0]["IndexOrder"];
                }
                catch { }


                return WebCls;
            }
        }


        public DataTable Reading()
        {
            string Query = "select top 6 * from SODCWebLinks order by IndexOrder";
            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;

        }

        public DataTable ReadingItems(string GroupLinkId)
        {
            string Query = "select * from tablegrouplinkitem where fk_GroupLink_Id = @GroupLinkId order by IndexOrder";
            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@GroupLinkId", SqlDbType.NVarChar);
            sqlCommand.Parameters["@GroupLinkId"].Value = GroupLinkId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;

        }

        public string AddWebLink(SqlTransaction sqlTransaction, string belongSite, string ActiveLanguage, string WebLinkName, string WebLinkUrl)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string WebId = System.Guid.NewGuid().ToString();
            string Query = "Insert into SodcWebLinks(WebLinkId,WebLinkName,WebLinkUrl,IndexOrder,belongSite) values (@WebId,@WebLinkName,@WebLinkUrl,@IndexOrder,@belongSite)";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@WebId", SqlDbType.VarChar);
            sqlCommand.Parameters["@WebId"].Value = WebId;

            sqlCommand.Parameters.Add("@WebLinkName", SqlDbType.NVarChar);
            sqlCommand.Parameters["@WebLinkName"].Value = WebLinkName;




            sqlCommand.Parameters.Add("@WebLinkUrl", SqlDbType.NVarChar);
            sqlCommand.Parameters["@WebLinkUrl"].Value = WebLinkUrl;

            sqlCommand.Parameters.Add("@belongSite", SqlDbType.NVarChar);
            sqlCommand.Parameters["@belongSite"].Value = belongSite;

            int IndexOder = 0;
            sqlCommand.Parameters.Add("@IndexOrder", SqlDbType.Int);
            sqlCommand.Parameters["@IndexOrder"].Value = IndexOder;
            try
            {
                sqlCommand.ExecuteNonQuery();
                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
            return WebId;

        }

        public WebLinkCls CreateModel(string ActiveLanguage, string WebLinkId)
        {
            string Query = "select * from SODCWebLinks where WebLinkId=@WebLinkId";

            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Parameters.Add("@WebLinkId", SqlDbType.VarChar);
            sqlCommand.Parameters["@WebLinkId"].Value = WebLinkId;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            if (dtResult.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                WebLinkCls WebCls = new WebLinkCls();
                WebCls.WebLinkId = (string)dtResult.Rows[0]["WebLinkId"];

                try
                {

                    WebCls.belongSite = (string)dtResult.Rows[0]["belongsite"];
                }
                catch { }

                try
                {
                    WebCls.WeblinkName = (string)dtResult.Rows[0]["WeblinkName"];
                }
                catch { }

                try
                {
                    WebCls.WebLinkUrl = (string)dtResult.Rows[0]["WebLinkUrl"];
                }
                catch { }

                try
                {
                    WebCls.IndexOrder = (int)dtResult.Rows[0]["IndexOrder"];

                }
                catch { }


                return WebCls;
            }
        }



        public void UpdateWebLink(SqlTransaction sqlTransaction, string ActiveLanguage, string WebId, string WebName, string WebUrl, int Index)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            string Query = "Update SODCWebLinks set WebLinkName = @WebName,WebLinkUrl = @WebUrl,IndexOrder = @Index where WebLinkID=@WebID";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@WebID", SqlDbType.VarChar);
            sqlCommand.Parameters["@WebID"].Value = WebId;
            sqlCommand.Parameters.Add("@WebName", SqlDbType.NVarChar);
            sqlCommand.Parameters["@WebName"].Value = WebName;

            sqlCommand.Parameters.Add("@WebUrl", SqlDbType.NVarChar);
            sqlCommand.Parameters["@WebUrl"].Value = WebUrl;
            sqlCommand.Parameters.Add("@Index", SqlDbType.Int);
            sqlCommand.Parameters["@Index"].Value = Index;
            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }

        public void DeleteWebLink(SqlTransaction sqlTransaction, string WebID)
        {
            bool HasTrans = sqlTransaction != null;
            SqlConnection sqlConnection = null;
            if (HasTrans == false)
            {
                sqlConnection = new SqlConnection(Environments.ConnectionString);
                sqlConnection.Open();
                sqlTransaction = sqlConnection.BeginTransaction();
            }
            else
            {
                sqlConnection = sqlTransaction.Connection;
            }

            // Environment.Permission.DeletePermission(sqlTransaction, AskID);
            string Query = " Delete from SODCWebLinks where WebLinkId=@WebID";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            sqlCommand.Transaction = sqlTransaction;
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add("@WebID", SqlDbType.VarChar);
            sqlCommand.Parameters["@WebID"].Value = WebID;

            try
            {
                sqlCommand.ExecuteNonQuery();

                if (HasTrans == false)
                {
                    sqlTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                if (HasTrans == false)
                {
                    sqlTransaction.Rollback();
                }
                throw (ex);
            }
        }



        public DataTable ReadingForService(string belongSite, string ActiveLanguage)
        {
            string Query = "select Id=GroupLinkId,Subject=[" + ActiveLanguage + "Subject] from TableGroupLink where belongSite='" + belongSite + "'";
            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }
    }
}
